<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/12/12
 * Time: 16:26
 */

namespace api\storage\model;

use think\Model;

class ContainersModel extends Model
{
    protected $table = 'iwh_containers';
    protected $pk = 'uuid';

    /**
     * 根据容器code来查找容器
     *
     * @param $containerCode
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function findByContainerCode($containerCode){
        return $this->where('container_code',$containerCode)->find();
    }

    public function getContainerReels($containerCode = '',$storageUuid = ''){
        return $this->alias('C')
                    ->join('iwh_reels R','R.container_code = C.container_code','left')
                    ->where(['C.container_code'=>$containerCode,
                            'R.storage_uuid'=>$storageUuid])
                    ->field('C.container_code,sub_materiel_id,reel_id,count')
                    ->order('reel_id')
                    ->select();

    }

    /**
     * 更新容器
     *
     * @param $containerCode
     * @param $data
     * @return false|int
     */
    public function updateByCode($containerCode,$data){
        return $this->isUpdate(true)->save($data,['container_code'=>$containerCode]);
    }

    /**
     * 更新flag
     *
     * @param $containerCode
     * @param $flag
     * @return false|int
     */
    public function updateFlag($containerCode, $flag)
    {
        $where['container_code'] = $containerCode;
        $data['flag'] = $flag;
        $ret = $this->isUpdate(true)->save($data, $where);
        $ret = $ret === false ? false : true;
        return $ret;
    }

    /**
     * 查询该站位上的容器号
     *
     * @param $seatCode
     * @param $storageUuid
     * @return array
     */
    public function queryContainers($seatCode, $storageUuid)
    {
        return $this->distinct(true)
            ->where('seat_code', $seatCode)
            ->where('storage_uuid', $storageUuid)
            ->order('container_code adc')
            ->column('container_code');
    }

}